﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class FlyItemObject : MonoBehaviour
{
    public Weapon weapon { get; set; }
    public int flyItemID { get; set; }

    public bool isRecycled = false;
    public ExplotionObject m_ExplotionEffect ;
    Ship m_EmitShip;
    protected float m_FlyDistance=0;
    protected Vector3 m_EmitStartPos;
    string m_ExplotionEffectName ="Bo";
    float m_CountTime = 0;
    private void Update()
    {
        m_FlyDistance += (transform.position - m_EmitStartPos).magnitude;
        m_EmitStartPos = transform.position;
       if (!isRecycled && (m_FlyDistance > weapon.flyItems[flyItemID].shotRange))
        {
            FlyItemPool.Instance.Recycle(this);
        }

        m_CountTime += Time.deltaTime;
        if(m_CountTime > FlyItemPool.RecycleMaxTime)
        {
            OnExplotion();
        }
    }
    public void OnExplotion()
    {
        m_CountTime = 0;
        FlyItemPool.Instance.Recycle(this);
        if(m_ExplotionEffect)
        {
            var effect = Instantiate(m_ExplotionEffect);
            var explosionScale = weapon.GetSelectedFlyItem().explotionScale;
            effect.transform.position = transform.position;
            effect.SetParticleScale(effect.gameObject, explosionScale);
            effect.Play();
        }
    }


    public void OnCollisionEnter2D(Collision2D collision)
    {
        OnExplotion();
    }
    public void CountStart()
    {
        m_CountTime = 0;
    }
    public static FlyItemObject Create(Weapon weapon, int flyItemID)
    {
        GameObject flyItemObj = new GameObject();
        var spriteRender = flyItemObj.AddComponent<SpriteRenderer>();
        FlyItemObject flyItemCom = null;
        switch (weapon.m_Type)
        {
            case Item.ItemType.ARTILLARY:
                flyItemCom  = flyItemObj.AddComponent<FlyItemArtilleryObject>();
                break;
        }
        if (flyItemCom == null)
            return null;
        flyItemCom.weapon = weapon;
        flyItemCom.flyItemID = flyItemID;
        spriteRender.sprite = ResourcesManager.Instance.GetWeaponsFlyItemSprite(weapon.typeID, flyItemID);
        spriteRender.sortingOrder = GM.flyItemRenderSort;
        var boxCollider = flyItemObj.AddComponent<BoxCollider2D>();
        var realPixelSize = GetSpriteSize(spriteRender.sprite);
        boxCollider.size = new Vector2(realPixelSize.x / spriteRender.sprite.pixelsPerUnit, realPixelSize.y / spriteRender.sprite.pixelsPerUnit);
        var rigiBody = flyItemObj.AddComponent<Rigidbody2D>();
        rigiBody.mass = weapon.flyItems[flyItemID].mass;
        rigiBody.gravityScale = 0;
        return flyItemCom;
    }

    /// <summary>
    ///  返回图像真正占用的矩形尺寸，单位是像素
    /// </summary>
    /// <param name="sprite"></param>
    /// <returns></returns>
    static Vector2Int GetSpriteSize(Sprite sprite)
    {
        var texture = sprite.texture;

        int startX = 0, endX = 0;
        int startY = 0, endY = 0;
        for(int i=0; i< texture.width; ++i)
        {
            Color color = new Color();
            for(int j=0; j<texture.height; ++j)
            {
                color = texture.GetPixel(i, j);
                if (color.a != 0)
                {
                    if (startY == 0)
                        startY = j;
                    if (j > endY)
                        endY = j;


                    if (startX == 0)
                        startX = i;
                    if (i > endX)
                        endX = i;
                }
            }
        }
        return new Vector2Int(endX-startX, endY-startY);
    }
    public void Init(Ship ship)
    {
        m_EmitShip = ship;
        m_ExplotionEffect = ResourcesManager.Instance.GetExplotionEffectPrefab(m_ExplotionEffectName);
        Physics2D.IgnoreCollision(GetComponent<Collider2D>(), m_EmitShip.GetComponent<Collider2D>());
    }
    public void RemoveIgnoreCollision()
    {
        if (m_EmitShip == null)
            return;
        Physics2D.IgnoreCollision(GetComponent<Collider2D>(), m_EmitShip.GetComponent<Collider2D>(), false);
    }
    public virtual  void Emit(WeaponObject weaponObj)
    {
    }
} 